package com.chenju.invokechainservice.adapter;

import com.chenju.invokechainservice.utils.FunnyListsUtils;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.tree.ClassNode;
import org.springframework.asm.Opcodes;

import java.util.ArrayList;
import java.util.List;

public class ClassNodeAdapter extends ClassNode {
    public List<MethodNodeAdapter> methods;

    public ClassNodeAdapter(){
        super(Opcodes.ASM9);
        this.methods = new ArrayList<>();
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        this.version = version;
        this.access = access;
        this.name = name;
        this.signature = signature;
        this.superName = superName.startsWith("com/chenju") ? superName : null;
        this.interfaces = FunnyListsUtils.asArrayList(interfaces);
        this.interfaces.removeIf(str -> !str.startsWith("com/chenju"));
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
//        String parsedMethodName = StringsUtils.findRealMethodNameFromLambdaMethod(name);
        MethodNodeAdapter method = new MethodNodeAdapter(access, name, descriptor, signature, exceptions);
        if (!"<clinit>".equals(name) && !"<init>".equals(name) && this.name.startsWith("com/chenju")){
            if (!this.methods.contains(method)){
                this.methods.add(method);
            }
        }
        return method;
    }

    @Override
    public void visitEnd() {
        super.visitEnd();
    }
}
